<template>
    <div class="app-container">

        <el-dialog :close-on-click-modal="false" :title="title" :visible="open" @close="cancel" width="950px" append-to-body>
            <!-- 项目信息表单 -->
            <template v-if="form.pid">
                <el-divider content-position="left"><strong>项目信息</strong></el-divider>
                <el-form :modal="projectForm" disabled ref="projectForm" size="small" :inline="true" label-width="68px">
                    <el-form-item label="客户" prop="custom">
                        <el-input v-model="projectForm.custom" placeholder="请输入客户" />
                    </el-form-item>
                    <el-form-item label="机型" prop="model">
                        <el-input v-model="projectForm.model" placeholder="请输入机型" />
                    </el-form-item>
                    <el-form-item label="项目编码" prop="projectNo">
                        <el-input v-model="projectForm.projectNo" placeholder="请输入项目编码" />
                    </el-form-item>
                    <el-form-item label="专案类别" prop="level">
                        <el-input v-model="projectForm.level" placeholder="请输入专案类别" />
                    </el-form-item>
                    <el-form-item label="芯片组" prop="chipGroup">
                        <el-input v-model="projectForm.chipGroup" placeholder="请输入芯片组" />
                    </el-form-item>
                    <el-form-item label="AD板" prop="adBoard">
                        <el-input v-model="projectForm.adBoard" placeholder="请输入AD板" />
                    </el-form-item>
                    <el-form-item label="LCM/OC" prop="lcmOc">
                        <el-input v-model="projectForm.lcmOc" placeholder="请输入LCM/OC" />
                    </el-form-item>
                    <el-form-item label="电源/适配器" class="labelword" prop="powerSupply">
                        <el-input v-model="projectForm.powerSupply" placeholder="请输入电源/适配器" />
                    </el-form-item>
                    <el-form-item label="恒流板" prop="currentBoard">
                        <el-input v-model="projectForm.currentBoard" placeholder="请输入恒流板" />
                    </el-form-item>
                    <el-form-item label="Arm板" prop="armBoard">
                        <el-input v-model="projectForm.armBoard" placeholder="请输入Arm板" />
                    </el-form-item>
                </el-form>
            </template>

            <el-divider content-position="left"><strong>bug信息</strong></el-divider>

            <el-row :gutter="70">
                <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                    <template v-show="!pid">
                        <el-col :span="7">
                            <el-form-item label="项目编码" prop="pid">
                                <el-select filterable @change="selectChange($event)" v-model="form.pid"
                                    placeholder="请选择对应的项目">
                                    <el-option v-for="item in projectLists" :key="item.id" :label="item.projectNo"
                                        :value="item.id"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </template>
                    <el-col :span="7">
                        <el-form-item label="产品阶段" prop="productStage">
                            <el-select filterable clearable v-model="form.productStage" placeholder="请选择产品阶段">
                                <el-option v-for="dict in dict.type.product_stage" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="问题类别" prop="problemType">
                            <el-select filterable clearable v-model="form.problemType" placeholder="请选择问题类别">
                                <el-option v-for="dict in dict.type.problem_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="严重程度" prop="level">
                            <el-select filterable clearable v-model="form.level" placeholder="请选择严重程度">
                                <el-option v-for="dict in dict.type.problem_level" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="设备型号" prop="modelCode">
                            <el-input v-model="form.modelCode" clearable placeholder="请输入设备型号" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="不良率" prop="badPersentage">
                            <el-input v-model="form.badPersentage" clearable placeholder="请输入不良率" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="验证版本" prop="bugVersion">
                            <el-input v-model="form.bugVersion" clearable placeholder="请输入验证版本" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="异常归类" prop="exceptionType">
                            <el-select filterable clearable v-model="form.exceptionType" placeholder="请选择异常归类">
                                <el-option v-for="dict in dict.type.exception_type" :key="dict.value" :label="dict.label"
                                    :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="异常代码" prop="exceptionCode">
                            <el-select filterable clearable v-model="form.exceptionCode" placeholder="请选择异常代码">
                                <el-option v-for="item in exceptionCodeList" :key="item.id"
                                    :label="`${item.code} ${item.type}`" :value="item.code"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="指派人" prop="rdHandle">
                            <el-select filterable multiple clearable v-model="form.rdHandle" placeholder="请选择指派人">
                                <el-option v-for="item in rdHandleList" :key="item.userId" :label="item.nickName"
                                    :value="item.userId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="电源/适配器标识" class="labelword" prop="powerSuppleId">
                            <el-input v-model="form.powerSuppleId" placeholder="请输入电源/适配器标识" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item label="lcm/oc标识" class="labelword" prop="lcmOcId">
                            <el-input v-model="form.lcmOcId" placeholder="请输入lcm/oc标识" />
                        </el-form-item>
                    </el-col>

                    <!-- <el-col :span="6">
                    <el-form-item label="上传附件" prop="fileId">
                        <el-button type="success">选择文件</el-button>
                    </el-form-item>
                </el-col> -->

                    <el-col>
                        <el-form-item label="标题" prop="bugTitle">
                            <el-input v-model="form.bugTitle" clearable placeholder="请输入标题" />
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="描述" prop="descriptions">
                            <editor :min-height="300" v-model.trim="form.descriptions" />
                        </el-form-item>
                    </el-col>
                    <!-- <el-form-item >
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </el-form-item> -->
                </el-form>
                <el-col>
                    <div style="width: 100%;text-align: center;" v-show="buttonView">
                        <el-button type="primary" @click="submitForm">确 定</el-button>
                        <el-button @click="cancel">取 消</el-button>
                    </div>
                </el-col>

            </el-row>
        </el-dialog>

    </div>
</template>
  
<script>
import { listBugs, getBugs, addBugs, updateBugs } from "@/api/business/bugs"
import { getProjectDetail, projectList } from "@/api/business/project";
import { getUserByPostCode, getUsersByRolekey } from "@/api/system/user";
import { listCodeData } from "@/api/business/code";
export default {
    name: 'Copy',
    dicts: ['problem_type', 'product_stage', 'problem_level', 'exception_type'],
    props: {
        // open: {
        //     type: Boolean,
        //     default: false,
        // },
        title: {
            type: String,
            default: '',
        },
        // isUpdate: {
        //     type: Boolean,
        //     default: false,
        // },
    },
    data() {
        return {
            exceptionCodeList: [],//异常code列表
            buttonView: true,
            projectLists: [],//项目数据列表
            // pid: null,//项目id
            rdHandleList: [],//指派人数据列表 
            open: false,//是否打开弹窗
            form: {},//表单数据对象
            projectForm: {},//项目表单数据对象
            rules: {
                pid: [
                    { required: true, message: "项目编码不能为空", trigger: "blur" }
                ],
                // bugTitle: [
                //     { required: true, message: "标题不能为空！", trigger: "blur" }
                // ],
                bugVersion: [
                    { required: true, message: "验证版本不能为空！", trigger: "blur" }
                ],
                // exceptionType: [
                //     { required: true, message: "异常归类不能为空！", trigger: "blur" }
                // ],
                productStage: [
                    { required: true, message: "产品阶段不能为空", trigger: "blur" }
                ],
                badPersentage: [
                    { required: true, message: "不良率不能为空", trigger: "blur" }
                ],
                level: [
                    { required: true, message: "严重程度不能为空", trigger: "blur" }
                ],
                rdHandle: [
                    { required: true, message: "指派人不能为空", trigger: "blur" }
                ],
                adBoard: [
                    { required: true, message: "AD板不能为空", trigger: "blur" }
                ],
                lcmOc: [
                    { required: true, message: "lcmOc不能为空", trigger: "blur" }
                ],
                powerSupply: [
                    { required: true, message: "电源/适配器不能为空", trigger: "blur" }
                ],
                currentBoard: [
                    { required: true, message: "恒流板不能为空", trigger: "blur" }
                ],
                armBoard: [
                    { required: true, message: "Arm板不能为空", trigger: "blur" }
                ],
                pjm: [
                    { required: true, message: "项目经理不能为空", trigger: "blur" }
                ],
                problemType: [
                    { required: true, message: "问题类别不能为空", trigger: "blur" }
                ],
                descriptions: [
                    { required: true, message: "问题描述不能为空", trigger: "blur" }
                ],
            },
        }
    },
    created() {
        this.reset()
        // this.pid = this.$route.query.id
        // if (this.pid) {
        //     this.form.pid = this.pid
        //     //获取项目详情
        //     this.getProjectFormDate(this.pid);
        // }
        this.getProjectList()
        this.getRdHandleList()
        this.getExceptionCode()
        // this.getPjmUserList()
        // this.getHardUserList()
        // this.getOpticsUserList()
        // this.getStuUserList()
        // this.getNPIUserList()
        // this.getSoftUserList()
    },
    mounted() {

    },
    methods: {
        /**
         * 打开bug复制弹窗
         * @param {*} bid 
         */
        onOpen(bid) {
            if (bid) {
                this.getBugsForm(bid)
            }
            this.open = true
        },
        /**
         * 获取bug详情
         * @param {*} id 
         */
        getBugsForm(id) {
            getBugs(id).then(response => {
                this.form = response.data
                this.form.bugsStatus = null
                // console.log(this.form)
                if (this.form.pid) {
                    // this.form.pid = this.pid
                    //获取项目详情
                    this.getProjectFormDate(this.form.pid);
                }
            });
        },
        selectChange(event) {
            this.getProjectFormDate(event);
        },
        //获取项目表单数据
        getProjectFormDate(id) {
            getProjectDetail(id).then(result => {
                //已经关闭的项目不展示
                if (result.data.status != 20) {
                    this.projectForm = result.data;
                } else {
                    this.form.pid = null
                }
            })
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                pid: null,
                bugCode: null,
                identify: null,
                level: null,
                modelCode: null,
                badPersentage: null,
                bugTitle: null,
                problemType: null,
                bugVersion: null,
                exceptionCode: null,
                respon: null,
                rdHandle: [],
                productStage: null,
                // remark: null,
                descriptions: undefined,
            };
            this.resetForm("form");
        },
        // 取消按钮
        cancel() {
            this.reset()
            this.open = false
        },
        /**新增bug提交按钮 */
        submitForm() {
            // console.log(this.form.descriptions)
            if (this.form.descriptions == '<p><br></p>') {
                this.$message.error("请输入邮件的内容！")
            } else {
                this.buttonView = false
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        this.$modal.loading("正在处理请稍等....")
                        addBugs(this.form).then(response => {
                            this.$modal.msgSuccess("创建成功")
                            this.$modal.closeLoading()
                            this.$emit("updateList")
                            this.cancel()
                        });
                        this.$modal.closeLoading()
                    }
                    this.buttonView = true
                });
            }
        },
        /**
         * 获取指派人列表(rd,npi)
         */
        getRdHandleList() {
            let rdList = []
            let npiList = []
            getUsersByRolekey(this.$store.state.globalParam.RDROLE).then(result => {
                rdList = result.data
                getUsersByRolekey(this.$store.state.globalParam.NPI).then(result => {
                    npiList = result.data
                    this.rdHandleList = rdList.concat(npiList)
                })

            })

        },
        // /**
        //  * 获取项目经理列表
        //  */
        // getPjmUserList() {
        //     getUserByPostCode(this.$store.state.globalParam.PJM).then(result => {
        //         this.pjmList = result.data
        //     })
        // },
        // /**
        //  * 获取硬件工程师列表
        //  */
        // getHardUserList() {
        //     getUserByPostCode(this.$store.state.globalParam.HARD).then(result => {
        //         this.hardList = result.data
        //     })
        // },
        // /**
        // * 获取软件工程师列表
        // */
        // getSoftUserList() {
        //     getUserByPostCode(this.$store.state.globalParam.SOFT).then(result => {
        //         this.softList = result.data
        //     })
        // },
        // /**
        // * 获取光学工程师列表
        // */
        // getOpticsUserList() {
        //     getUserByPostCode(this.$store.state.globalParam.OPTICS).then(result => {
        //         this.opticsList = result.data
        //     })
        // },
        // /**
        // * 获取结构工程师列表
        // */
        // getStuUserList() {
        //     getUserByPostCode(this.$store.state.globalParam.STU).then(result => {
        //         this.stuList = result.data
        //     })
        // },
        // /**
        // * 获取工厂NPI列表
        // */
        // getNPIUserList() {
        //     getUserByPostCode(this.$store.state.globalParam.NPI).then(result => {
        //         this.NPIList = result.data
        //     })
        // },
        //获取项目详情
        getProjectBYId(id) {
            getProjectDetail(id).then(result => {
                this.form = result.data
            })
        },
        /**
        * 获取系统所有的项目数据
        */
        getProjectList() {
            projectList().then(result => {
                this.projectLists = result.data;
            })
        },
        /**
         * 获取系统异常代码列表
         */
        getExceptionCode() {
            listCodeData().then(result => {
                this.exceptionCodeList = result.data;
            })
        },
    }
}
</script>
  
<style lang="scss" scoped>
// .el-col {
//     height: 60px;
// }
.labelword ::v-deep .el-form-item__label {
    white-space: pre-line !important;
    line-height: 15px !important;
}

.app-container {
    overflow-x: hidden;
    overflow-y: auto;
}
</style>
  